﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 生成数据值 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        long ins = Stopwatch(测试);

        Response.Write("用时:" + ins + (200000 / ins) + "/s" + "<br/>");
    }

    #region 性能测试

    void 测试()
    {
        DateTime dt = DateTime.Now.AddMilliseconds(0.1);
        int n = 0;
        while (n < 200000)
        {

            long s = GetNumber();
            Response.Write(s+",<br/>");
            n++;
        }
         
    }

    /// <summary>
    /// 测试运行时间
    /// </summary>
    /// <param name="method">需要执行的方法</param>
    /// <param name="summary">对方法的说明</param>
    /// <returns>返回,这个方法执行用了多少毫秒</returns>
    public long Stopwatch(Action method)
    {
        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Restart();
        //运行方法
        method.Invoke();

        watch.Stop();

        return watch.ElapsedMilliseconds;
    }

    #endregion

    /// <summary>
    /// 当前分钟
    /// </summary>
    private int curMM = -1;
    /// <summary>
    /// 获取一个用于数据库索引的数字
    /// </summary>
    public long GetNumber()
    {
        int mm = DateTime.Now.Minute;
        //int mm = date.Minute;
        if (curMM != mm)
        {
            //设置值
            System.Threading.Interlocked.Exchange(ref curMM, mm);
            System.Threading.Interlocked.Exchange(ref _SEQ, 0);
        }

        long result = long.Parse(DateTime.Now.ToString("yyyyMMddHH") + mm + GetIndexNumber());
        return result;
    }

    private int _SEQ = 0;

    /// <summary>
    /// 获取索引序号
    /// </summary>
    /// <returns></returns>
    public int GetIndexNumber()
    {
        System.Threading.Interlocked.Increment(ref _SEQ);
        if (_SEQ > 999999)
        {
            System.Threading.Interlocked.Exchange(ref _SEQ, 1);
        }
        return _SEQ;

    }
}